<?php
namespace App\Service\Admin;

use Illuminate\Support\Facades\DB;

class ImageService extends BaseService
{
    //图片

    //列表
    public function getList($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = DB::table($this->dbImg);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return $this->toback(-2, '没有数据');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $arys[] = [
                'link' => $v['link'],
                'w' => $v['width'],
                'h' => $v['height'],
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 添加
     * @param string $img 图片
     * @param int $w 宽度
     * @param int $h 高度
     */
    public function addOne($img, $w, $h)
    {
        $arys = [
            'link' => $img,
            'width' => $w,
            'height' => $h,
            'addtime' => toDate(),
        ];
        $lastid = DB::table($this->dbImg)->insertGetId($arys);
        if (!$lastid) {
            return array('code' => -2, 'msg' => 'error');
        }
        return array('code' => 0, 'msg' => 'success');
    }
}